<template>
	<view>
		<view class="search-box">
			 <my-search @gotoSearch="gotoSearch"></my-search>
		</view>
		<swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000" circular>
			<swiper-item v-for="(item,index) in swiperList" :key="item.goods_id">
				<navigator class="swiper-item" :url="`/subpkg/goods_detail/goods_detail?goods_id=`+item.goods_id">
					<image :src="item.image_src" alt=""></image>
				</navigator>
			</swiper-item>
		</swiper>
		<view class="nav-list">
			<view class="nav-item" v-for="(item,index) in navList" :key="index" @click="navClickHandler(item)">
				<image :src="item.image_src" mode=""></image>
			</view>
		</view>
		<view class="floor-list">
			<view class="floor-item" v-for="(item,index) in floorList" :key="index">
				<image :src="item.floor_title.image_src" class="floor-title"></image>
				<view class="floor-image-box">
					<navigator class="floor-left-box" :url="item.product_list[0].url">
						<image :src="item.product_list[0].image_src" mode="widthFix"
							:style="`width:`+item.product_list[0].image_width+`rpx`"></image>
					</navigator>
					<view class="floor-right-box">
						<navigator v-for="(item2,index2) in item.product_list.slice(1)" :key="index2" :url="item2.url">
							<image :src="item2.image_src" mode="widthFix" :style="`width:`+item2.image_width+`rpx`">
							</image>
						</navigator>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import badgeMixin from "@/mixins/cart-badge.js"
	export default {
		mixins: [badgeMixin],
		data() {
			return {
				swiperList: [],
				navList: [],
				floorList: []

			};
		},
		mounted() {
			this.getSwiperList();
			this.getNavList();
			this.getFloorList();
		},
		methods: {
			async getSwiperList() {
				let res = await uni.$http.get("/home/swiperdata")
				if (res.data.meta.status !== 200) return uni.$showMsg();
				this.swiperList = res.data.message;
			},
			async getNavList() {
				let {
					data: res
				} = await uni.$http.get("/home/catitems")
				if (res.meta.status !== 200) return uni.$showMsg();
				this.navList = res.message;
			},
			async getFloorList() {
				let {
					data: res
				} = await uni.$http.get("/home/floordata")
				if (res.meta.status !== 200) return uni.$showMsg();
				// 接口返回的供路由跳转的url参数不对，要添加一个自定义属性url
				res.message.forEach(floor => {
					floor.product_list.forEach(prod => {
						prod.url = "/subpkg/goods_list/goods_list?" + prod.navigator_url.split("?")[1];
					})
				})
				this.floorList = res.message;
			},
			navClickHandler(item) {
				if (item.name == "分类") {
					uni.switchTab({
						url: "/pages/cate/cate"
					})
				}
			},
			gotoSearch() {
				uni.navigateTo({
					url: "/subpkg/search/search"
				})
			}
		}
	}
</script>

<style lang="scss">
	// 实现搜索框的吸顶效果
	.search-box {
		position: sticky;
		top: 0;
		z-index: 999;
	}

	swiper {
		height: 330rpx;

		.swiper-item,
		image {
			width: 100%;
			height: 100%;
		}
	}

	.nav-list {
		display: flex;
		justify-content: space-around;
		margin: 15px 0;

		image {
			width: 128rpx;
			height: 140rpx;
		}
	}

	.floor-title {
		width: 100%;
		height: 60rpx;
	}

	.floor-image-box {
		display: flex;
		padding-left: 10rpx;

		.floor-right-box {
			display: flex;
			flex-wrap: wrap;
			justify-content: space-around;
		}
	}
</style>